package com.zkingsoft.actions.admin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.model.Client;
import com.zkingsoft.model.ProjPost;
import com.zkingsoft.model.Project;
import com.zkingsoft.model.Task;
import com.zkingsoft.model.Users;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.ClientService;
import com.zkingsoft.services.ProjPostService;
import com.zkingsoft.services.ProjUserService;
import com.zkingsoft.services.ProjectService;
import com.zkingsoft.services.TaskService;
import com.zkingsoft.services.UsersService;

/**
 * @date 2016-11-09 09:41
 */
@Controller
@RequestMapping(value = "admin/project")
public class ProjectController extends BaseController {

	@Resource
	private ProjectService currentService;

	@Resource
	private ClientService clientService;

	@Resource
	private ProjPostService projPostService;

	@Resource
	private UsersService usersService;

	@Resource
	private ProjUserService projUserService;
	@Resource
	private TaskService taskService;

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(Project project, PaginationVO pageVo) {
		Long id = getMe().getId();
		if (Dictionary.USER_ROLE_ZJL.equals(getMe().getRole().getName())) {
			id = null;
		}
		pageVo.setOrder("desc");
		pageVo.setSort("createTime");
		return new AjaxResult(AjaxResult.STATUS_OK, null, null,
				currentService.findByUserId(id, project, pageVo),
				currentService.findByUserIdCount(id, project));

	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Project project) {
		if (project.getId() != null) {
			return modify(currentService, project, "项目");
		} else {
			return add(currentService, project, "项目");
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		Project project;
		if (id != null) {
			project = currentService.findById(id);
			WebUtil.getRequest().setAttribute("obj", project);
			WebUtil.getRequest().setAttribute("count", project.getProjUserList().size());
		} else {
			WebUtil.getRequest().setAttribute("count", 1);
		}
		// 查询项目客户
		List<Client> clientList = clientService.findByModel(null);
		WebUtil.getRequest().setAttribute("clientList", clientList);
		// 查询职位
		List<ProjPost> postList = projPostService.findByModel(null);
		WebUtil.getRequest().setAttribute("postList", postList);
		// 查询人员
		Users userQuery = new Users();
		userQuery.setUserType(Dictionary.USER_TYPE_EMPLOYEE);
		userQuery.setIsDeleted("N");
		List<Users> usersList = usersService.findByModel(userQuery);
		WebUtil.getRequest().setAttribute("usersList", usersList);

		// 查询项目下的与员工
		return "admin/project-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	/**
	 * 删除项目关联的员工 @Title: del @Description: @author:罗凯 @param keys @return
	 * AjaxResult 返回类型 @date 2016年11月13日 下午4:59:01 @throws
	 */
	@RequestMapping(value = "/delProjUser")
	public @ResponseBody AjaxResult delProjUser(String keys) {
		return remove(projUserService, keys);
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/manager")
	public String manager(Long id) {
		Project project;
		if (id != null) {
			project = currentService.findById(id);
			WebUtil.getRequest().setAttribute("obj", project);
		}
		return "admin/project-manager";
	}

	/**
	 * @Description:查询某个项目的总任务数（已开始，未开始，已完成，且没有被删除的）
	 * @author:dingchuan
	 * @param project
	 * @return 返回类型 AjaxResult
	 * @date 2016年12月20日
	 */
	@RequestMapping(value = "/queryAllTask")
	public @ResponseBody AjaxResult queryAllTask(Long projId) {
		List<Task> tasks = taskService.findProjTaskAll(projId);
		if (tasks != null) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "成功", tasks, tasks.size());
		}
		return new AjaxResult(AjaxResult.STATUS_ERR, null, "失败");
	}

	/**
	 * @Description:查询某个项目的已完成任务数（已完成，且没有被删除的）
	 * @author:dingchuan
	 * @param project
	 * @return 返回类型 AjaxResult
	 * @date 2016年12月20日
	 */
	@RequestMapping(value = "/queryAllFinishTask")
	public @ResponseBody AjaxResult queryAllFinishTask(Long projId) {
		List<Task> tasks = taskService.findProjFinishTask(projId);
		if (tasks != null) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "成功", tasks, tasks.size());
		}
		return new AjaxResult(AjaxResult.STATUS_ERR, null, "失败");
	}

	/*******************************************************
	 * 外部接口方法
	 */

	/**
	 * 查询所有的项目
	 */
	@RequestMapping(value = "/showListAll")
	public @ResponseBody AjaxResult showList(Project project) {
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, currentService.findByModel(project), 0);
	}

}